package TCPSocket;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import net.sf.json.JSONObject;
import java.io.StringReader;

/**
 * Hiermee kun je alarmgrenzen voor sensoren versturen in JSON, moet nog wel een consturctor in die ipadress en port meegeeft als params.
 * @author David de Hoop
 */
public class GUISocket {
    String ServerInetAddress = "77.162.98.183";
    int portnumber = 81;
    
    public String SensorBoundary(String sensorname, double lowerboundary, double upperboundary) {
        JSONObject SensorBoundaryValues = new JSONObject();
        
		 
	SensorBoundaryValues.put("sensorname", sensorname);
	SensorBoundaryValues.put("lowerboundary", lowerboundary);
	SensorBoundaryValues.put("upperboundary", upperboundary);
        
        JSONObject SensorBoundaries = new JSONObject();
        SensorBoundaries.put("analogAlarmChange", SensorBoundaryValues);
        
        String JSONString = SensorBoundaries.toString();
                
        return JSONString;
    }
    
    public String SensorBoundaryBinary(String sensorname, boolean alarmstatus) {
        JSONObject SensorBoundaryValues = new JSONObject();
        
	SensorBoundaryValues.put("sensorname", sensorname);
	SensorBoundaryValues.put("alarm", alarmstatus);
	
        JSONObject SensorBoundaries = new JSONObject();
        SensorBoundaries.put("binaryAlarmChange", SensorBoundaryValues);
        
        String JSONString = SensorBoundaries.toString();
                
        return JSONString;
    }
    
    
    public void TCPSocket(String JSONObject) throws IOException {
        Socket GUISocket = new Socket(ServerInetAddress,portnumber);
                 
        DataOutputStream ToServer = new DataOutputStream(GUISocket.getOutputStream());
        ToServer.writeBytes(JSONObject);
        ToServer.close();
        GUISocket.close();
    }
    
     /**
     * @param args the command line arguments
     */
public static void main(String [ ] args) throws IOException
{
    GUISocket test = new GUISocket();
      
    test.TCPSocket(test.SensorBoundary("AnalogSensor1",45.5, 74));


}
}
